const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        show: false,
        //微信登陆凭证
        wechat:'',
        referrer_id: 0,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        if (options.referrer_id){
            this.setData({
                referrer_id:options.referrer_id
            })
        }
    },

    onShow() {
        console.log('邀请人ID：' + this.data.referrer_id);
    },
    cancel() {
        wx.navigateBack()
        // app.switchTab('/pages/mine/index');
    },
    getPhoneNumber(e) {
        let _this = this;
        _this.setData({
            show: !_this.data.show,
        })
        if (!e.detail.encryptedData) return false;
        app.HttpsRequest('user.Register/login', 'POST', {
            encryptedData: e.detail.encryptedData,
            iv: e.detail.iv,
            wechat:_this.data.wechat,
            referrer_id: _this.data.referrer_id
        }).then(ret => {
            if(ret){
                wx.setStorageSync('token',ret.data.token);
                wx.navigateBack();
            }
        })
    },
    getUserInfo(ret) {
        let that = this;
        app.loading('正在处理...');
        wx.login({
            success(loginRes) {
                if (loginRes.code) {
                    wx.getUserInfo({
                        success(user) {
                            console.log(user)
                            app.HttpsRequest('user.wechat/auth', 'POST', {
                                code: loginRes.code,
                                encryptedData: user.encryptedData,
                                iv: user.iv
                            }).then(ret => {
                                if (ret) {
                                    wx.hideLoading();
                                    that.setData({
                                        show: !that.data.show,
                                        wechat: ret.data.wechat
                                    });
                                    wx.setStorageSync('wechat', JSON.stringify(user.userInfo));
                                }
                            });
                        },
                        fail(err) {
                            app.tips('拒绝授权会导致部分功能不可用', 3000)
                        }

                    })
                }
            }
        })
    },
    /**
     * 授权登录
     */
    authorLogin: function(e) {

    },
})